/**
 *	@file	safari_l.c
 *	@brief	サファリレイアウトツール
 *	@author	Miyuki Iwasawa
 *	@date	08.03.15
 */

#include "common.h"
#include "system/procsys.h"
#include "system/arc_util.h"
#include "system/arc_tool.h"
#include "system/brightness.h"
#include "system/bmp_list.h"
#include "system/bmp_menu.h"
#include "system/window.h"
#include "system/pm_str.h"
#include "system/msgdata.h"
#include "system/fontproc.h"
#include "system/snd_tool.h"
#include "system/clact_util.h"
#include "system/clact_tool.h"
#include "system/render_oam.h"
#include "system/wipe.h"
#include "system/wordset.h"
#include "system/buflen.h"
#include "gflib/msg_print.h"
#include "gflib/strbuf_family.h"
#include "field/sysflag.h"
#include "field/eventflag.h"
#include "application/app_tool.h"
#include "savedata/config.h"

#include "safari/safari_sys.h"
#include "application/safari_l.h"
#include "safaril_sys.h"
#include "safaril_sub.h"
#include "safaril_act.h"

#include "msgdata/msg.naix"


//================================================================
//----------------------------------------------------------------
///定義
//----------------------------------------------------------------
//================================================================



//================================================================
//----------------------------------------------------------------
///プロトタイプ
//----------------------------------------------------------------
//================================================================
static void sub_InitDataSet(SAFARIL_WORK* wk,SAFARI_LTPARAM* sp);
static void sub_LastDataSet(SAFARIL_WORK* wk);

static int seq_Init(SAFARIL_WORK* wk);
static int seq_End(SAFARIL_WORK* wk);
static int seq_ModeBlock(SAFARIL_WORK* wk);
static int seq_ModeChange(SAFARIL_WORK* wk);
static int seq_ModeSort(SAFARIL_WORK* wk);
static int seq_FadeIn(SAFARIL_WORK* wk);
static int seq_FadeOut(SAFARIL_WORK* wk);

//================================================================
//----------------------------------------------------------------
///プログラムエリア
//----------------------------------------------------------------
//================================================================

//----------------------------------------------------------------
///グローバルエリア
//----------------------------------------------------------------
/**
 *	@brief	レイアウトツール プロセス初期化
 *	@param	proc	プロセスデータ
 *	@param	seq		シーケンス
 *
 *	@return	処理状況
 */
PROC_RESULT SafariLProc_Init( PROC *proc,int *seq)
{
	SAFARIL_WORK *wk = NULL;
	SAFARI_LTPARAM * sp;

	sp = (SAFARI_LTPARAM*)PROC_GetParentWork(proc);

	//ワークエリア取得
	HeapStatePush();

	//ヒープ作成
	sys_CreateHeap(HEAPID_BASE_APP,HEAPID_SAFARI_APP,0x50000);

	wk = PROC_AllocWork( proc,sizeof(SAFARIL_WORK),HEAPID_SAFARI_APP);
	MI_CpuClear8(wk,sizeof(SAFARIL_WORK));

	//パラメータ引継ぎ
	wk->heapID = HEAPID_SAFARI_APP;

	sub_InitDataSet(wk,sp);
	Snd_DataSetByScene( SND_SCENE_SUB_SAFARI, 0, 0 ); //サウンドデータロード(BGM引継ぎ)
	return PROC_RES_FINISH;
}

/**
 *	@brief	レイアウトツール　プロセス終了
 *	@param	proc	プロセスデータ
 *	@param	seq		シーケンス
 *
 *	@return	処理状況
 */
PROC_RESULT SafariLProc_End( PROC *proc,int *seq)
{
	int heapID;
	SAFARIL_WORK* wk = PROC_GetWork(proc);

	//データセット
	sub_LastDataSet(wk);
	heapID = wk->heapID;

	//ワークエリア解放
	MI_CpuClear8(wk,sizeof(SAFARIL_WORK));
	PROC_FreeWork(proc);

	HeapStatePop();
	HeapStateCheck(heapID);
	sys_DeleteHeap(heapID);

	return PROC_RES_FINISH;
}


/***
 *	@brief	レイアウトツール　メインプロセス
 */
PROC_RESULT	SafariLProc_Main( PROC *proc,int* seq)
{
	int end_f = 0;
	SAFARIL_WORK* wk = PROC_GetWork(proc);

	switch(*seq){
	case SEQ_INIT:
		*seq = seq_Init(wk);
		break;
	case SEQ_END:
		*seq = seq_End(wk);
		break;
	case SEQ_MODE_BLOCK:
		*seq = seq_ModeBlock(wk);
		break;
	case SEQ_MODE_CHANGE:
		*seq = seq_ModeChange(wk);
		break;
	case SEQ_MODE_SORT:
		*seq = seq_ModeSort(wk);
		break;
	case SEQ_FADEIN:
		*seq = seq_FadeIn(wk);
		break;
	case SEQ_FADEOUT:
		*seq = seq_FadeOut(wk);
		break;
	case SEQ_EXIT:
		return PROC_RES_FINISH;
	default:
		break;
	}
	return PROC_RES_CONTINUE;
}


/////////////////////////////////////////////////////////////////////////
//ローカル


/*
 *	@brief	アプリ初期化データセット
 */
static void sub_InitDataSet(SAFARIL_WORK* wk,SAFARI_LTPARAM* sp)
{
	CONFIG* config_sv;

	wk->safari_sv = SaveData_GetSafariSave(sp->save);
	SafariSv_GetLayout(wk->safari_sv,SAFARI_LAYOUT_MINE,&wk->layout);

	config_sv = SaveData_GetConfig(sp->save);
	wk->msg_spd	= CONFIG_GetMsgSpeed(config_sv);
	wk->win_type = CONFIG_GetWindowType(config_sv);

	//現在のキー動作モードを取得
	wk->key_mode = KeyTouchStatus_CheckTouchOrKey((KEYTOUCH_STATUS*)sp->kt_status);

	//カスタマイズフラグを初期化
	wk->custom_f = FALSE;

	wk->param = sp;

	//配置物のカスタマイズができる状態か？
	if(SafariSv_GetObjLevel(wk->safari_sv) > SAFARI_OBJ_LEVEL0 ){
		wk->obj_place_f = TRUE;
	}
//	wk->obj_place_f = TRUE;
}

/*
 *	@brief	アプリセーブデータ反映
 */
static void sub_LastDataSet(SAFARIL_WORK* wk)
{
	//レイアウトデータ書き戻し
	SafariSv_SetLayout(wk->safari_sv,SAFARI_LAYOUT_MINE,(const SAFARI_LAYOUT*)&wk->layout);

	//カスタマイズしたかのフラグをセット
	SysFlag_SafariBlockCustomSet(SaveData_GetEventWork(wk->param->save),wk->custom_f);

	//現在のキー動作モードを書き戻し
	KeyTouchStatus_SetTouchOrKey((KEYTOUCH_STATUS*)wk->param->kt_status,wk->key_mode);
}


/**
 *	@brief	アプリ初期化
 */
static int seq_Init(SAFARIL_WORK* wk)
{
	if(!SafariLSeq_AppInit(wk)){
		return SEQ_INIT;
	}
	return SEQ_FADEIN;
}

/*
 *	@brief	アプリ終了
 */
static int seq_End(SAFARIL_WORK* wk)
{
	if(!SafariLSeq_AppEnd(wk)){
		return SEQ_END;
	}
	return SEQ_EXIT;
}

/*
 *	@brief	ブロック選択メイン
 */
static int seq_ModeBlock(SAFARIL_WORK* wk)
{
	int ret;

	switch(wk->seq){
	case LSEQ_NULL:
		wk->seq = SafariLBlock_Input(wk);
		break;
	case LSEQ_LIST_SEL:
		wk->seq = SafariLBlock_ListSelect(wk);
		break;
	case LSEQ_MODE_SHIFT:
		wk->seq = SafariLEff_ModeBCShift(wk);
		break;
	case LSEQ_END:
		wk->seq = 0;
		SafariL_ActorMain(wk);
		return wk->next_seq;
	}
	SafariL_ActorMain(wk);
	return SEQ_MODE_BLOCK;
}

/*
 *	@brief	ブロック取替えメイン
 */
static int seq_ModeChange(SAFARIL_WORK* wk)
{
	int ret;

	switch(wk->seq){
	case LSEQ_NULL:
		wk->seq = SafariLChange_Input(wk);
		break;
	case LSEQ_LIST_SEL:
		wk->seq = SafariLChange_ListSelect(wk);
		break;
	case LSEQ_MODE_SHIFT:
		wk->seq = SafariLEff_ModeBCShift(wk);
		break;
	case LSEQ_EFF:
		wk->seq = SafariLChange_PageScroll(wk);
		break;
	case LSEQ_END:
		wk->seq = 0;
		SafariL_ActorMain(wk);
		return wk->next_seq;
	}
	SafariL_ActorMain(wk);
	return SEQ_MODE_CHANGE;
}

/*
 *	@brief	ブロックソートメイン
 */
static int seq_ModeSort(SAFARIL_WORK* wk)
{
	int ret;

	switch(wk->seq){
	case LSEQ_NULL:
		wk->seq = SafariLSort_Input(wk);
		break;
	case LSEQ_MODE_SHIFT:
		wk->seq = SafariLEff_ModeBCShift(wk);
		break;
	case LSEQ_EFF:
		wk->seq = SafariLSort_PanelChange(wk);
		break;
	case LSEQ_END:
		wk->seq = 0;
		SafariL_ActorMain(wk);
		return wk->next_seq;
	}
	SafariL_ActorMain(wk);
	return SEQ_MODE_SORT;
}

/**
 *	@brief	フェードイン
 */
static int seq_FadeIn(SAFARIL_WORK* wk)
{
	switch(wk->seq){
	case 0:
		WIPE_SYS_Start(WIPE_PATTERN_WMS,
				WIPE_TYPE_FADEIN,
				WIPE_TYPE_FADEIN,0x0000,COMM_BRIGHTNESS_SYNC,1,wk->heapID
				);
		GF_Disp_GX_VisibleControl( GX_PLANEMASK_OBJ, VISIBLE_ON );
		GF_Disp_GXS_VisibleControl( GX_PLANEMASK_OBJ, VISIBLE_ON );
		++wk->seq;
		break;
	case 1:
		if(!WIPE_SYS_EndCheck()){
			break;
		}
		wk->seq = 0;
		SafariL_ActorMain(wk);
		return SEQ_MODE_BLOCK;
	}
	SafariL_ActorMain(wk);
	return SEQ_FADEIN;
}

/**
 *	@brief	フェードアウト
 */
static int seq_FadeOut(SAFARIL_WORK* wk)
{

	switch(wk->seq){
	case 0:
		WIPE_SYS_Start(WIPE_PATTERN_WMS,
				WIPE_TYPE_FADEOUT,
				WIPE_TYPE_FADEOUT,0x0000,COMM_BRIGHTNESS_SYNC,1,wk->heapID
				);
		++wk->seq;
		break;
	case 1:
		if(!WIPE_SYS_EndCheck()){
			break;
		}
		wk->seq = 0;
		SafariL_ActorMain(wk);
		return SEQ_END;
	}
	SafariL_ActorMain(wk);
	return SEQ_FADEOUT;
}